package com.cz.common.utils;

import com.cz.modules.rll.entity.RllHourCountEntity;
import com.cz.modules.rll.entity.RllPlaceEntity;
import com.cz.modules.rll.service.RllPlaceService;
import com.sun.jna.Pointer;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HCKNetSDKUtil {
    @Autowired
    private static RllPlaceService rllPlaceService;
    static HCNetSDK hCNetSDK = HCNetSDK.INSTANCE;
    public static Map<String,Integer> lUserIDMap=new HashMap<>();

    public static Integer login(String ip){
        hCNetSDK.NET_DVR_Init();
        HCNetSDK.NET_DVR_DEVICEINFO_V30 m_strDeviceInfo = new HCNetSDK.NET_DVR_DEVICEINFO_V30();
        Integer lUserID = hCNetSDK.NET_DVR_Login_V30(ip,
                (short) 8000, "admin", "YQFWQ123", m_strDeviceInfo);
        if (lUserID == -1){ System.out.println("登录失败，错误码为"+hCNetSDK.NET_DVR_GetLastError());
        }else{
            System.out.println("登录成功！");
        }
        return lUserID;
    }
    //获取客流
    public static List<RllHourCountEntity> getPDCResult(String ip,Integer lUserID, String startTime, String endTime) throws ParseException {
        List<RllHourCountEntity> hourCountList=new ArrayList<>();
        if(lUserID!=-1){
            HCNetSDK.NET_DVR_PDC_QUERY_COND m_struPdcResultCond = new HCNetSDK.NET_DVR_PDC_QUERY_COND();
            m_struPdcResultCond.read();
            m_struPdcResultCond.dwSize = m_struPdcResultCond.size();
            m_struPdcResultCond.dwChannel = 1;
            //处理时间
            String startArray[]=startTime.split(" ");
            String startDate[]=startArray[0].split("-");
            String startT[]=startArray[1].split(":");

            m_struPdcResultCond.struStartTime.wYear = new Short(startDate[0]);
            m_struPdcResultCond.struStartTime.byMonth = new Byte(startDate[1]);
            m_struPdcResultCond.struStartTime.byDay = new Byte(startDate[2]);
            m_struPdcResultCond.struStartTime.byHour = new Byte(startT[0]);
            m_struPdcResultCond.struStartTime.byMinute = new Byte(startT[1]);
            m_struPdcResultCond.struStartTime.bySecond = new Byte(startT[2]);

            //处理时间
           // System.err.println("---endTime-----------"+endTime);
            String endArray[]=endTime.split(" ");
            String endDate[]=endArray[0].split("-");
            String endT[]=endArray[1].split(":");

            m_struPdcResultCond.struEndTime.wYear = new Short(endDate[0]);
            m_struPdcResultCond.struEndTime.byMonth = new Byte(endDate[1]);
            m_struPdcResultCond.struEndTime.byDay = new Byte(endDate[2]);
            m_struPdcResultCond.struEndTime.byHour = new Byte(endT[0]);
            m_struPdcResultCond.struEndTime.byMinute = new Byte(endT[1]);
            m_struPdcResultCond.struEndTime.bySecond = new Byte(endT[2]);

            m_struPdcResultCond.byReportType = 1;
            m_struPdcResultCond.write();
            Pointer lpInBuffer = m_struPdcResultCond.getPointer();
            int lHandle = hCNetSDK.NET_DVR_StartRemoteConfig(lUserID,HCNetSDK.NET_DVR_GET_PDC_RESULT, lpInBuffer,
                    m_struPdcResultCond.size(), null, null);
            if (lHandle < 0) {
             //   System.out.println("建立NET_DVR_GET_PDC_RESULT长连接失败，错误号：" + hCNetSDK.NET_DVR_GetLastError());
                Integer ll=login(ip);
                lUserIDMap.put(ip,ll);
            }
            else{
               // System.out.println("建立NET_DVR_GET_PDC_RESULT长连接成功");
                HCNetSDK.NET_DVR_PDC_RESULT m_struPdcResult = new HCNetSDK.NET_DVR_PDC_RESULT();
                m_struPdcResult.write();
                Pointer pPdcResult = m_struPdcResult.getPointer();
                int iRetSult = 0;
                while (true) {
                    iRetSult = hCNetSDK.NET_DVR_GetNextRemoteConfig(lHandle, pPdcResult, m_struPdcResult.size());

                    if (iRetSult == 1000) {
                        //System.out.println("hCNetSDK.NET_DVR_GetLastError():"+hCNetSDK.NET_DVR_GetLastError());
                        //System.out.println("NET_DVR_GetNextRemoteConfig 状态  ：" + iRetSult);
                        m_struPdcResult.read();
                    /*System.out.println("-------开始时间:" + m_struPdcResult.struStartTime.wYear + "-"+ m_struPdcResult.struStartTime.byMonth
                            +"-"+m_struPdcResult.struStartTime.byDay+"-" + m_struPdcResult.struStartTime.byHour+"-"+m_struPdcResult.struStartTime.byMinute
                            +"-"+m_struPdcResult.struStartTime.bySecond+",结束时间:" + m_struPdcResult.struEndTime.wYear + "-"+ m_struPdcResult.struEndTime.byMonth
                            +"-"+m_struPdcResult.struEndTime.byDay+"-" + m_struPdcResult.struEndTime.byHour+"-"+m_struPdcResult.struEndTime.byMinute
                            +"-"+m_struPdcResult.struEndTime.bySecond + "-------");
                    System.out.println("进入人数:"+m_struPdcResult.dwEnterNum + ",离开人数:"+m_struPdcResult.dwLeaveNum);*/
                        String stTime=m_struPdcResult.struStartTime.wYear + "-"+ m_struPdcResult.struStartTime.byMonth
                                +"-"+m_struPdcResult.struStartTime.byDay+" 0" + m_struPdcResult.struStartTime.byHour+":0"+m_struPdcResult.struStartTime.byMinute
                                +":0"+m_struPdcResult.struStartTime.bySecond;
                        String eTime=m_struPdcResult.struEndTime.wYear + "-"+ m_struPdcResult.struEndTime.byMonth
                                +"-"+m_struPdcResult.struEndTime.byDay+" " + m_struPdcResult.struEndTime.byHour+":"+m_struPdcResult.struEndTime.byMinute
                                +":"+m_struPdcResult.struEndTime.bySecond;
                        RllHourCountEntity hourCount=new RllHourCountEntity();
                        hourCount.setStartTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(stTime));
                        hourCount.setEndTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:SS").parse(eTime));
                        hourCount.setEnterNum(m_struPdcResult.dwEnterNum);
                        hourCount.setLeaveNum(m_struPdcResult.dwLeaveNum);
                        hourCountList.add(hourCount);

                    } else if (iRetSult == 1001) {
                        //System.out.println("hCNetSDK.NET_DVR_GetLastError():"+hCNetSDK.NET_DVR_GetLastError());
                        //System.out.println("NET_DVR_GetNextRemoteConfig 状态  ：" + iRetSult);
                    } else if (iRetSult == 1002 || iRetSult == 1003) {
                        //System.out.println("hCNetSDK.NET_DVR_GetLastError():"+hCNetSDK.NET_DVR_GetLastError());
                        //System.out.println("NET_DVR_GetNextRemoteConfig 状态  ：" + iRetSult);

                        hCNetSDK.NET_DVR_StopRemoteConfig(lHandle);
                        break;
                    }
                }
            }
        }
        return hourCountList;
    }

}
